﻿using CinemaMaestro.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace CinemaMaestro.WindowsApplication.EndPoints
{
    public class ActorEndPoints
    {
        private const string ActorsUrl = "api/actors";
        private HttpClient client;

        public ActorEndPoints(HttpClient client)
        {
            this.client = client;
        }

        public async Task<List<ActorViewModel>> GetAllActorsAsync()
        {
            List<ActorViewModel> actors = new List<ActorViewModel>();
            HttpResponseMessage response = await client.GetAsync(ActorsUrl);
            if (response.IsSuccessStatusCode)
            {
                actors = await response.Content.ReadAsAsync<List<ActorViewModel>>();
            }
            return actors;
        }

        public async Task<ActorViewModel> GetActorAsync(int id)
        {
            ActorViewModel actor = null;
            HttpResponseMessage response = await client.GetAsync(ActorsUrl + "?id=" + id);
            if (response.IsSuccessStatusCode)
            {
                actor = await response.Content.ReadAsAsync<ActorViewModel>();
            }
            return actor;
        }

        public async Task CreateActorAsync(ActorViewModel actor)
        {
            HttpResponseMessage response = await client.PostAsJsonAsync(ActorsUrl, actor);
            response.EnsureSuccessStatusCode();
        }

        public async Task UpdateActorAsync(ActorViewModel actor)
        {
            HttpResponseMessage response = await client.PutAsJsonAsync(ActorsUrl, actor);
            response.EnsureSuccessStatusCode();
        }

        public async Task DeleteActorAsync(int id)
        {
            HttpResponseMessage response = await client.DeleteAsync(ActorsUrl + "?id=" + id);
            response.EnsureSuccessStatusCode();
        }
    }
}
